#!/usr/bin/env python
"""
See LICENSE.txt for licensing details (GPLv2).
Copyright (c) 2008 Rune M. Friborg, runef@diku.dk.
"""

from plugins.system import *


class PluginManagerEXECUTION(PluginManager):
    def __init__(self):
        PluginManager.__init__(self, 'EXECUTION')
        
    def load(self):

        import plugins.execute.workspaces
        plugins.execute.workspaces.load(self)

        import plugins.execute.multiplier
        plugins.execute.multiplier.load(self)

        import plugins.execute.broadcast
        plugins.execute.broadcast.load(self)
        
        import plugins.execute.setup
        plugins.execute.setup.load(self)

        import plugins.execute.forknjoin
        plugins.execute.forknjoin.load(self)

    def getCommandLineHelp(self):
        help = ''
        for p in self.plugins:
            help += p.getCommandLineHelp()
        return help

    def parseCommandLineArgs(self, args):
        for p in self.plugins:
            args = p.parseCommandLineArgs(args)
        return args

    def editProcessTreePlugin(self, root):
        for p in self.plugins:
            root = p.editProcessTree(root)
        return root

    def editProcessCreation(self, processObj, L, D):
        for p in self.plugins:
            (L, D) = p.editProcessCreation(processObj, L, D)
        return (L, D)

    def editReducedSet(self, channelObjList, processObjList):
        for p in self.plugins:
            (channelObjList, processObjList) = p.editReducedSet(channelObjList, processObjList)
        return (channelObjList, processObjList)

    def loadTags(self, node_def, elem, att):
        for p in self.plugins:
            att = p.loadTags(node_def, elem, att)
        return att

    def getList(self, list_def):
        L = []
        for p in self.plugins:
            L = p.getList(list_def, L)
        return L
    
    def updatePath(self, L):
        for p in self.plugins:
            L = p.updatePath(L)
        return L

    def preserveAttributes(self, new_plugin_attr, old_plugin_attr):
        for p in self.plugins:
            p.preserveAttributes(new_plugin_attr, old_plugin_attr)


class PluginEXECUTION(Plugin):
    def __init__(self, manager, name):
        Plugin.__init__(self, manager, name)
        self.CommandLine_help = ''

    # Request
    def getCommandLineHelp(self):
        return self.CommandLine_help

    # Request
    def parseCommandLineArgs(self, args):
        for (m1,m2,callback,has_value) in self.getListCallback('CommandLineArg'):
            for arg in args[:]:
                if (m1 == arg or m2 == arg):
                    if (has_value):
                        value_index = args.index(arg)+1
                        if (len(args) > value_index):
                            callback(args[value_index])
                            
                            del args[value_index]
                            del args[value_index-1]
                    else:
                        callback()
                        del args[args.index(arg)]
        return args
        
    # Add
    def addCommandLineArg(self, m1, m2, callback, helptext, has_value=False):
        self.CommandLine_help += helptext
        e = (m1,m2,callback,has_value)
        self.addListCallback('CommandLineArg', e)

    # Request
    def editProcessTree(self, rootnode):
        root = rootnode
        for callback in self.getListCallback('ProcessTree'):
            root = callback(rootnode)
        return root

    # Add
    def addCSPTreeProcessing(self, callback):
        self.addListCallback('ProcessTree', callback)

    # Request
    def editProcessCreation(self, processObj, L, D):
        for callback in self.getListCallback('ProcessCreation'):
            (L, D) = callback(processObj, L, D)
        return (L, D)

    # Add
    def addToProcessCreation(self, callback):
        self.addListCallback('ProcessCreation', callback)

    # Request
    def editReducedSet(self, channelObjList, processObjList):
        for callback in self.getListCallback('ReducedSetProcessor'):
            (channelObjList, processObjList)=  callback(channelObjList, processObjList)
        return (channelObjList, processObjList)

    # Add
    def addReducedSetProcessor(self, callback):
        self.addListCallback('ReducedSetProcessor', callback)

    # Request
    def loadTags(self, node_def, node, att):
        for callback in self.getDictCallback('Tags', node_def):
            att = callback(node, att)
        return att

    # Add
    def addCSPFileProcessing(self, node_def, callback):
        self.addDictCallback('Tags', node_def, callback)

    # Request
    def getList(self, list_def, L):
        for callback in self.getDictCallback('List', list_def):
            L = callback(L)
        return L

    # Add
    def addList(self, list_def, callback):
        self.addDictCallback('List', list_def, callback)

    # Request
    def updatePath(self, L):
        for callback in self.getListCallback('UpdatePath'):
            L = callback(L)
        return L

    # Add
    def addUpdatePath(self, callback):
        self.addListCallback('UpdatePath', callback)

    # Request
    def preserveAttributes(self, new_plugin_attr, old_plugin_attr):
        for callback in self.getListCallback('PreserveAttributes'):
            callback(new_plugin_attr, old_plugin_attr)

    # Add
    def addPreserveAttributes(self, callback):
        self.addListCallback('PreserveAttributes', callback)

